class RenderIndex {
    constructor(){
        this.$navBox = document.querySelector('.function-img')
        this.init()
    }
    init = () => {
        this.formatNavList()
    }
    // 初始化导航栏
    formatNavList = () => {
        // 先发起请求
        fetch('http://chst.vip:1234/api/getindexmenu').then(res => res.json()).then(res => {
            let str = res.result.map(item => {
                // console.log(item);
                // http://chst.vip:8081/for-stu/wfzh/images/ic_search.png
                // <img src=\"images/ic_bcj.png\" alt=\"白菜价\">
                // http://chst.vip:8081/for-stu/wfzh/images/ic_bcj.png、

                // 重置图片地址
                let img = item.img.replace(/"([a-zA-Z0-9_.-\\]+\/[a-zA-Z0-9_.-\\]+)"/, (...args) => {
                    return "http://chst.vip:8081/for-stu/wfzh/" + args[1]
                })
                return `
                <div class="fti-box" titlehref=${item.titlehref}>
                    <div class="ftext">
                        ${img}
                    </div>
                    <p>${item.name}</p>
                </div>
                `
            }).join('')
            this.$navBox.innerHTML = str
            this.addNavLiEvent()
        })
    }

    // 给导航栏的每一个li都添加点击事件
    addNavLiEvent = () => {
        Array.from(this.$navBox.children).forEach(item => {
            item.onclick = function(){
                location.href = '/views/' + this.getAttribute('titlehref') + "?productid=20"
            }
        })
    }
}

new RenderIndex()